package net.hmapp.app.activity;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.FrameLayout;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.FragmentTransaction;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import net.hmapp.app.fragment.CartFragment;
import net.hmapp.app.fragment.CatalogFragment;
import net.hmapp.app.fragment.HomeFragment;
import net.hmapp.app.fragment.MineFragment;

public class MainActivity extends AppCompatActivity {


    FrameLayout frameLayout;
    HomeFragment homeFragment;
    CartFragment cartFragment;
    CatalogFragment catalogFragment;
    MineFragment mineFragment;
    BottomNavigationView bottomNavigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        frameLayout = findViewById(R.id.main_content);
        bottomNavigationView = findViewById(R.id.nav_menu_bar);
        SelectItemBar(0);
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
               if (item.getItemId()==R.id.menu_home){
                   SelectItemBar(0);
                   return true;
               }
                if (item.getItemId()==R.id.menu_catalog){
                    SelectItemBar(1);
                    return true;
                }
                if (item.getItemId()==R.id.menu_cart){
                    SelectItemBar(2);
                    return true;
                }
                if (item.getItemId()==R.id.menu_mine){
                    SelectItemBar(3);
                    return true;
                }
                return false;
            }
        });

    }


    //排他思想
    public void hideFragment(FragmentTransaction fragmentTransaction) {
        if (homeFragment != null) {
            fragmentTransaction.hide(homeFragment);
        }
        if (cartFragment != null) {
            fragmentTransaction.hide(cartFragment);
        }
        if (catalogFragment != null) {
            fragmentTransaction.hide(catalogFragment);
        }
        if (mineFragment != null) {
            fragmentTransaction.hide(mineFragment);
        }
    }

    public void SelectItemBar(int index) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        hideFragment(fragmentTransaction);
        if (index == 0) {
            if (homeFragment == null) {
                homeFragment = new HomeFragment();
                fragmentTransaction.add(R.id.main_content, homeFragment);
            } else {
                fragmentTransaction.show(homeFragment);
            }

        }
        if (index == 1) {
            if (catalogFragment == null) {
                catalogFragment = new CatalogFragment();
                fragmentTransaction.add(R.id.main_content, catalogFragment);
            } else {
                fragmentTransaction.show(catalogFragment);
            }
        }


        if (index == 2) {
            if (cartFragment == null) {
                cartFragment = new CartFragment();
                fragmentTransaction.add(R.id.main_content, cartFragment);
            } else {
                fragmentTransaction.show(cartFragment);
            }
        }

        if (index == 3) {
            if (mineFragment == null) {
                mineFragment = new MineFragment();
                fragmentTransaction.add(R.id.main_content, mineFragment);
            } else {
                fragmentTransaction.show(mineFragment);
            }
        }

        fragmentTransaction.commit();
    }


}